package cn.whx.hrm.service.impl;

import cn.hutool.http.HttpUtil;
import cn.whx.hrm.constant.AuthConstant;
import cn.whx.hrm.domain.LoginUser;
import cn.whx.hrm.domain.UserMeal;
import cn.whx.hrm.dto.LoginDto;
import cn.whx.hrm.dto.RefreshTokenDto;
import cn.whx.hrm.mapper.LoginUserMapper;
import cn.whx.hrm.mapper.UserMealMapper;
import cn.whx.hrm.properties.Oauth2ClientDetails;
import cn.whx.hrm.properties.Oauth2ClientDetailsProperties;
import cn.whx.hrm.service.ILoginUserService;
import cn.whx.hrm.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author whx
 * @since 2021-06-17
 */
@Service
public class LoginUserServiceImpl extends ServiceImpl<LoginUserMapper, LoginUser> implements ILoginUserService {
    @Autowired
    private UserMealMapper userMealMapper;
    @Autowired
    private Oauth2ClientDetailsProperties properties;

    @Override
    public boolean insert(LoginUser entity) {
        //保存loginuser表
        super.insert(entity);
        //保存userMeal表和MealId
        UserMeal userMeal = new UserMeal();
        userMeal.setMealId(entity.getMealId());
        userMeal.setLoginId(entity.getId());
        userMealMapper.insert(userMeal);

        return true;

    }
    /*  String url = "http://localhost:3010/oauth/token?client_id=%s&client_secret=%s&grant_type=password&redirect_uri=http://www.baidu.com&username=%s&password=%s";
      @Override
      public AjaxResult login(@Valid LoginDto dto) {
          //先判断用户类型然后给相对应的token
          if (dto.getType()==0){//后台管理员
              url = String.format(url,"admin",1,dto.getUsername(),dto.getPassword());
          }else {//前端
              url = String.format(url, "website",1,dto.getUsername(),dto.getPassword());
          }
          //发送请求
          String http = HttpUtil.get(url);
          Map map = JSON.parseObject(http, Map.class);
          return AjaxResult.me().setResultObj(map);
      }*/
    //解决硬编码的问题
    //http://localhost:3010/oauth/token?grant_type=refresh_token&refresh_token=6c16cadb-805f-4cef-958f-d6fb3a29f82c&client_id=admin&client_secret=1
    @Override
    public AjaxResult login(@Valid LoginDto dto) {
        //根据他的类型获取配置对象
        Oauth2ClientDetails clientDetials = properties.getClientDetials(dto.getType());
        //准备一个密码模式
        String url  = String.format(AuthConstant.PWD_AUTH_URL,clientDetials.getClientid(),clientDetials.getSecret(),dto.getUsername(),dto.getPassword());
        //发送请求
        String http = HttpUtil.get(url);
        Map map = JSON.parseObject(http, Map.class);
        return AjaxResult.me().setResultObj(map);
    }

    @Override
    public AjaxResult refresh(@Valid RefreshTokenDto dto) {
        //根据类型 获取配置对象
        Oauth2ClientDetails clientDetials = properties.getClientDetials(dto.getType());
        //准备密码模式
        String url = String.format(AuthConstant.PWD_AUTH_REFRESH_URL,dto.getRefreshToken(),clientDetials.getClientid(),clientDetials.getSecret());
        String http = HttpUtil.get(url);
        Map map = JSON.parseObject(http, Map.class);
        return AjaxResult.me().setResultObj(map);
    }
}
